/**
 * 日志工具模块
 * 提供统一的日志记录功能
 */

class Logger {
  constructor(prefix = '') {
    this.prefix = prefix;
  }

  /**
   * 格式化时间戳
   */
  _getTimestamp() {
    return new Date().toISOString();
  }

  /**
   * 格式化日志消息
   */
  _formatMessage(level, message, data = null) {
    const timestamp = this._getTimestamp();
    const prefix = this.prefix ? `[${this.prefix}]` : '';
    let logMessage = `[${timestamp}] ${prefix}[${level}] ${message}`;
    
    if (data) {
      logMessage += ` ${JSON.stringify(data)}`;
    }
    
    return logMessage;
  }

  /**
   * 信息日志
   */
  info(message, data) {
    const logMessage = this._formatMessage('INFO', message, data);
    console.log('\x1b[36m%s\x1b[0m', logMessage);
    return logMessage;
  }

  /**
   * 警告日志
   */
  warn(message, data) {
    const logMessage = this._formatMessage('WARN', message, data);
    console.warn('\x1b[33m%s\x1b[0m', logMessage);
    return logMessage;
  }

  /**
   * 错误日志
   */
  error(message, data) {
    const logMessage = this._formatMessage('ERROR', message, data);
    console.error('\x1b[31m%s\x1b[0m', logMessage);
    return logMessage;
  }

  /**
   * 调试日志
   */
  debug(message, data) {
    if (process.env.NODE_ENV === 'development' || process.env.DEBUG) {
      const logMessage = this._formatMessage('DEBUG', message, data);
      console.log('\x1b[90m%s\x1b[0m', logMessage);
      return logMessage;
    }
  }

  /**
   * 成功日志
   */
  success(message, data) {
    const logMessage = this._formatMessage('SUCCESS', message, data);
    console.log('\x1b[32m%s\x1b[0m', logMessage);
    return logMessage;
  }
}

module.exports = Logger;